#============================================================
#	Plugin
#============================================================
# @datetime: 2022-5-2 19:01:21
#============================================================
class_name _CodeEditor_SubPlugin_AddCommonts
extends _CodeEditor_SubPluginBase

const AddComments = preload("res://addons/code_editor_menu/plugin/comments/add_comments_.gd")


var add_comments = AddComments.new()
var popup: PopupMenu


#==================================================
#   自定义方法
#==================================================
#(override)
func install():
	.install()
	add_comments.set_plugin(get_plugin())


#(override)
func init_menu_button():
	.init_menu_button()
	var parse_shortcut = get_util().parse_shortcut_util
	# 添加菜单选项
	var idx = 0
	# 脚本信息
	idx = _menu_button.add_popup_item("脚本文件注释", self, "add_script_file_comment")
	# 方法注释
	idx = _menu_button.add_popup_item("方法注释", self, "add_method_comments")
	_menu_button.set_item_shortcut(idx, parse_shortcut.parse_shortcut("Ctrl + Shift + C"))
	# 分隔符
	idx = _menu_button.add_popup_item("分隔注释", self, "add_separator_comments")
	_menu_button.set_item_shortcut(idx, parse_shortcut.parse_shortcut("Ctrl + /"))
	# 代码块分类
	idx = _menu_button.add_popup_item("代码块分类注释", self, "add_classify_comments")
	_menu_button.set_item_shortcut(idx, parse_shortcut.parse_shortcut("Ctrl + Shift + /"))


#==================================================
#   功能方法
#==================================================
func get_text_editor() -> TextEdit:
	return get_util().script_editor_util.get_current_code_textedit()


## 添加分隔注释
func add_separator_comments():
	var text_edit = get_text_editor()
	if text_edit:
		var comments = "# {Sep} [  ] ".format({
			"Sep": "=".repeat(25)
		})
		text_edit.insert_text_at_cursor(comments)
		text_edit.cursor_set_column(30)


## 添加代码块分类注释
func add_classify_comments():
	var text_edit = get_text_editor()
	if text_edit:
		var comments = add_comments.get_classify_comments()
		text_edit.insert_text_at_cursor(comments)
		text_edit.cursor_set_line(text_edit.cursor_get_line() - 1)


## 添加方法注释
## @comments_method 调用的注释方法
func add_method_comments():
	var text_edit = get_text_editor()
	if text_edit:
		var comments = add_comments.get_cursor_next_method_comments()
		text_edit.insert_text_at_cursor(comments)


## 脚本文件信息注释
func add_script_file_comment():
	var comments = add_comments.get_script_info_comments()
	var text_edit = get_text_editor()
	if text_edit:
		text_edit.select(0, 0, 0, 0)
		text_edit.cursor_set_column(0)
		text_edit.cursor_set_line(0)
		text_edit.insert_text_at_cursor(comments)
		text_edit.cursor_set_line(3)
		text_edit.cursor_set_column(1000)

